﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion

using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;

namespace raya.Library
{
    public sealed class RayaSettings : IConfigurationSectionHandler
    {
        static RayaSettings() 
        {
            Object obj = ConfigurationManager.GetSection("Raya");
        }
        public Object Create(Object parent, object configContext, XmlNode section) 
        {
            NameValueCollection settings;

            try 
            {
                NameValueSectionHandler baseHandler = new NameValueSectionHandler();
                settings = (NameValueCollection) baseHandler.Create(parent, configContext, section);
            } 
            catch
            {
                settings = null;
            }

            if (settings != null) 
            {
                language = ReadSetting(settings, "language", "en_US");
                web_prefix = ReadSetting(settings, "web_prefix", "");
                container = ReadSetting(settings, "container", "default");
                ifr_uri = ReadSetting(settings, "ifr_uri", "/gadgets/ifr?");
                gadget_server = ReadSetting(settings, "gadget_server", "http://localhost/");
                st_max_age = ReadSetting(settings, "st_max_age", "3600");
                site_root = ReadSetting(settings, "site_root", "");
                enable_caching = string.Compare(bool.TrueString, ReadSetting(settings, "enable_caching", "true")) == 0;
                fb_api_key = ReadSetting(settings, "fb_api_key", "");
                fb_api_secret = ReadSetting(settings, "fb_api_secret", "");
                gfc_key = ReadSetting(settings, "gfc_key", "");
            }

            return null;
        }

        private static String ReadSetting(NameValueCollection settings, String key, String defaultValue)
        {
            if (settings == null || key == null)
                throw new ArgumentNullException();

            try
            {
                Object setting = settings[key];
                return (setting == null) ? defaultValue : (String)setting;
            }
            catch
            {
                return defaultValue;
            }
        }

        private static string language;
        private static string web_prefix;
        private static string container;
        private static string ifr_uri;
        private static string gadget_server;
        private static string st_max_age;
        private static string site_root;
        private static bool enable_caching;
        private static string fb_api_key;
        private static string fb_api_secret;
        private static string gfc_key;

        public static String Language 
        {
            get { return language; }
            set { language = value; }
        }
        public static String Web_prefix
        {
            get { return web_prefix; }
            set { web_prefix = value; }
        }
        public static String Container
        {
            get { return container; }
            set { container = value; }
        }
        public static String Ifr_uri
        {
            get { return ifr_uri; }
            set { ifr_uri = value; }
        }
        public static String Gadget_server
        {
            get { return gadget_server; }
            set { gadget_server = value; }
        }
        public static String St_max_age
        {
            get { return st_max_age; }
            set { st_max_age = value; }
        }

        public static String Site_root
        {
            get { return site_root; }
            set { site_root = value; }
        }

        public static bool Enable_caching
        {
            get { return enable_caching; }
            set { enable_caching = value; }
        }

        public static String Fb_api_key
        {
            get { return fb_api_key; }
            set { fb_api_key = value; }
        }

        public static String Fb_api_secret
        {
            get { return fb_api_secret; }
            set { fb_api_secret = value; }
        }

        public static String Gfc_key
        {
            get { return gfc_key; }
            set { gfc_key = value; }
        }
    }
}